<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Api extends CI_Controller 
{
		function index()
		{
			$this->load->model('common');
			$data['data']=json_decode(json_encode($this->common->Get('api')));
			$this->load->view('common/header');
			$this->load->view('api/index',($data));
			$this->load->view('common/footer');	
		}
		function update($param=NULL)
		{
			$this->load->model('common');
			//print_r($this->input->post());
			//return;
			if($this->input->post('save'))
			{
				
				$data=array('name'=>$this->input->post('api'));
				$rs=$this->common->GetById('api',$data);
				//print_r($rs);
				
				$data=array('name'=>$this->input->post('api'),'api_key'=>$this->input->post('key'));
				if($rs==NULL)
					$this->common->Update('api',$data);
				else
				{
					$id=array('id'=>$rs[0]->id);
					$this->common->Update('api',$data,0,$id);
				}
				//return;
				echo redirect(base_url().'index.php/api');
			}
			
			if($param==NULL)
				$data['data']=array();
			else
			{
				$data=array('name'=>$this->input->post('api'));
				$rs=json_decode(json_encode($this->common->GetById('api',$data)));
				if($rs)
				{
					$data['data']=$rs[0];
				}
				else
				{
					$data['data']=null;
				}
			}
			
			$this->load->view('common/header');
			$this->load->view('api/form',$data);
			$this->load->view('common/footer');
		}
		function delete($param=NULL)
		{
			$this->load->model('common');
			//print_r($this->input->post());
			//return;
			
			if($param==NULL)
			{
				//$data['data']=array();
				echo redirect(base_url().'index.php/api');
			}
			else
			{
				//$data['data']=$this->common->GetById('api',array('id'=>$param));
				//print_r($data['data'][0]);
				//$this->common->where('id',$param);
				$this->common->delete('api',array('id'=>$param));
			}
			echo redirect(base_url().'index.php/api');
			
		}
}